/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.bezeroa;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dBasea.Bezeroa;

public class BajaBezeroa extends Panela{
	private static final long serialVersionUID = -2813193093723935393L;
	
	private JLabel zenbakia;
	private JTextField nif;
	private JButton ok;
	private AbstractButton ezeztatu;
	private Bezeroa beze=new Bezeroa();
	public BajaBezeroa(String tituloa, final ItzuliInterfazea itzuliInterfazea) {		
		super(tituloa);

		JPanel erdikoPanela = this.getErdikoPanela();
		
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout());
		
		
		//Erdiko panelak
		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(7,1));
		
		JPanel g=new JPanel();
		g.setBackground(SystemColor.activeCaption);
		erdikoa.add(g);
		
		JPanel geg=new JPanel();
		geg.setBackground(SystemColor.activeCaption);
		erdikoa.add(geg);
		
		JPanel arri = new JPanel();
		arri.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arri = new FlowLayout();
		fl_arri.setHgap(15);
		fl_arri.setVgap(10);
		arri.setLayout(fl_arri);
		zenbakia=new JLabel("Bezero NIF:");
		zenbakia.setFont(new Font("Arial", Font.PLAIN, 14));
		nif=new JTextField(8);
		nif.addKeyListener(new KeyAdapter() {
			
			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length()>=9){
					arg0.consume();
					}
			}
		});
		arri.add(zenbakia);
		arri.add(nif);
		erdikoa.add(arri);
		
		
		
		JPanel d = new JPanel();
		d.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(10);
		d.setLayout(fl_arrib);
		erdikoa.add(d);
		
		JPanel e=new JPanel();
		e.setBackground(SystemColor.activeCaption);
		erdikoa.add(e);
		
		
		
		//beheko panela
		JPanel behekoa = new JPanel();
		FlowLayout flowLayout = (FlowLayout) behekoa.getLayout();
		flowLayout.setHgap(110);
		flowLayout.setVgap(10);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Baja eman");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);		
		
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try
                {
					if (!zerbaitIdatzi()){
	            		JOptionPane.showMessageDialog(null,
	            				"Sartu behar duzu zerbait zuriunetan\n"
	            						+ "Ezin da eremu hutsik egon", "Oker",
	            				JOptionPane.ERROR_MESSAGE);
					}
					else if (!beze.existitzenDa(nif.getText())) {            		
	            		JOptionPane.showMessageDialog(null,
	            				"Ez da bezero hori aurkitzen", "Oker",
	            				JOptionPane.ERROR_MESSAGE);
            		}
					else {
					beze.bezEzabatu((nif.getText()));
						JOptionPane.showMessageDialog(null,
								"Bezeroa kenduta!",
								"", JOptionPane.INFORMATION_MESSAGE);
						eremuakGarbitu();
            		}
                }
            	catch (Exception e) {
            		JOptionPane.showMessageDialog(null,
            				"Ezin izan da bezeroa kendu", "Oker",
            				JOptionPane.ERROR_MESSAGE);
            		e.printStackTrace();
                }}
		});
		ezeztatu.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	
	}
	
	private boolean zerbaitIdatzi(){
		boolean aurki;
		aurki=nif.getText().length() > 0;
   		return aurki;
   	}

	@Override
	public void eremuakGarbitu() {

		nif.setText("");
	}
}
